package com.liuxu.topnews.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Handler;

import com.liuxu.topnews.bean.Point;
import com.liuxu.topnews.bean.RGBInfo;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;

public class ColorUtils
{
    private static final int Distance = 150;

    public static double CIE_1976(double abs, double abs2, double abs3, final double n, final double n2, final double n3) {
        abs = Math.abs(n - abs);
        abs2 = Math.abs(n2 - abs2);
        abs3 = Math.abs(n3 - abs3);
        return Math.sqrt(Math.pow(abs, 2.0) + Math.pow(abs2, 2.0) + Math.pow(abs3, 2.0));
    }

    public static double CMC(double n, double sqrt, double atan, double n2, double abs, double abs2, double n3, double n4) {
        n2 = (n2 + n4) / 2.0;
        n4 = 0.5 * (1.0 - Math.sqrt(Math.pow(n2, 7.0) / (Math.pow(n2, 7.0) + Math.pow(25.0, 7.0))));
        sqrt *= 1.0 + n4;
        n2 = Math.sqrt(Math.pow(sqrt, 2.0) + Math.pow(atan, 2.0));
        atan = Math.atan(atan / sqrt);
        abs2 *= 1.0 + n4;
        sqrt = Math.sqrt(Math.pow(abs2, 2.0) + Math.pow(n3, 2.0));
        n3 = Math.atan(n3 / abs2);
        abs = Math.abs(n - abs);
        abs2 = Math.abs(n2 - sqrt);
        n4 = Math.abs(atan - n3);
        n3 = Math.sqrt(n2 * sqrt);
        n4 = Math.sin(n4 / 2.0);
        if (n >= 16.0) {
            sqrt = 0.040975 * n / (1.0 + 0.01765 * n);
        }
        else {
            sqrt = 0.511;
        }
        final double n5 = 0.0638 * n2 / (1.0 + 0.0131 * n2) + 0.638;
        n2 = Math.sqrt(Math.pow(n2, 4.0) / (Math.pow(n2, 4.0) + 1900.0));
        n = 0.0;
        boolean b;
        if (atan > 345.0) {
            b = true;
        }
        else {
            b = false;
        }
        boolean b2;
        if (atan < 164.0) {
            b2 = true;
        }
        else {
            b2 = false;
        }
        if (b2 | b) {
            n = 0.36 + Math.abs(0.4 * Math.cos(35.0 + atan));
        }
        else {
            boolean b3;
            if (atan >= 164.0) {
                b3 = true;
            }
            else {
                b3 = false;
            }
            boolean b4;
            if (atan <= 345.0) {
                b4 = true;
            }
            else {
                b4 = false;
            }
            if (b4 & b3) {
                n = 0.56 + Math.abs(0.2 * Math.cos(168.0 + atan));
            }
        }
        return Math.sqrt(Math.pow(abs / (1.4 * sqrt), 2.0) + Math.pow(abs2 / (1.0 * n5), 2.0) + Math.pow(2.0 * n3 * n4 / (n5 * (n2 * n + 1.0 - n2)), 2.0));
    }

    public static RGBInfo getRgb(final String s) {
        final int color = Color.parseColor(s);
        final int red = Color.red(color);
        final int green = Color.green(color);
        final int blue = Color.blue(color);
        return new RGBInfo(red, green, blue);
    }

    public static void getShoot() {
        final String string = "/sdcard/360/" + System.currentTimeMillis() + ".png";
        RootUtils.getInstance().addCommand("screencap -p " + string);
        new Handler().postDelayed((Runnable)new Runnable() {
            @Override
            public void run() {
                try {
                    final Bitmap decodeStream = BitmapFactory.decodeStream((InputStream)new FileInputStream(string));
                    final ArrayList<Point> list = new ArrayList<Point>();
                    for (int i = 0; i < 10; ++i) {
                        list.add(new Point(326, 1012));
                    }
                    for (final Point point : list) {
                        final int pixel = decodeStream.getPixel(point.x, point.y);
                    }
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }, 3000L);
    }

    public static boolean isColorSame(int n, int n2, int n3, final int n4, final int n5, final int n6) {
        n -= n4;
        n2 -= n5;
        n3 -= n6;
        final double sqrt = Math.sqrt(n * n + n2 * n2 + n3 * n3);
        return sqrt < 150.0;
    }
}
